package com.ins.service.impl;

import com.ins.common.utils.TimeAgoUtils;
import com.ins.model.entity.AppInfoMarket;
import com.ins.model.entity.AppNotice;
import com.ins.model.query.Query;
import com.ins.model.vo.ConfigsVO;
import com.ins.model.vo.RecommendInfoVO;
import com.ins.model.vo.RecommendVO;
import com.ins.service.AppInfoMarketService;
import com.ins.service.AppNoticeService;
import com.ins.service.HomeService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.bean.BeanUtil;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ins.model.entity.table.AppNoticeTableDef.APP_NOTICE;

/**
 * HomeServiceImpl
 *
 * @author aa
 * @version 1.0
 * @since 2024/7/15
 */
@Service
@RequiredArgsConstructor
public class HomeServiceImpl implements HomeService {

    private final AppNoticeService appNoticeService;

    private final AppInfoMarketService appInfoMarketService;

    @Override
    public ConfigsVO configs() {
        List<String> notices = appNoticeService.list(APP_NOTICE.STATUS.eq(1)).stream().map(AppNotice::getContent).toList();
        return new ConfigsVO(notices);
    }

    @Override
    public RecommendVO recommend() {
        return appInfoMarketService.recommend();
    }

    @Override
    public Page<RecommendInfoVO> search(Query query) {

        return appInfoMarketService.page(Page.of(query.getPage(), query.getLimit()),
                                           QueryWrapper.create().like(AppInfoMarket::getName, query.getKeyword()))
                                   .map(a -> {
                                       RecommendInfoVO riv = new RecommendInfoVO();
                                       BeanUtil.copyProperties(a, riv);
                                       riv.setAgoTimes(TimeAgoUtils.format(a.getCreateTime()));
                                       return riv;
                                   });
    }
}
